<?php
// 激活回调
// http://127.0.0.1:8098/jfq.php?tp=callback

$timeout = 20;

$query = exec_db("SELECT * FROM tttj.jfq_callback");
$list = array();
while ($row = mysqli_fetch_assoc($query)) {
    $id = intval($row['id']);
    $cid = intval($row['cid']);
    $appid = intval($row['appid']);
    $idfa = $row['idfa'];
    $callback_url = $row['callback_url'];
    $now = time();

    if(empty($callback_url)){//为空则直接激活生效
        exec_db("INSERT INTO tttj.jfq_active (cid,appid,idfa,ctime) VALUES ('{$cid}','{$appid}','{$idfa}','{$now}')");
        exec_db("DELETE FROM tttj.jfq_callback WHERE id = {$id} LIMIT 1");
        continue;
    }

    ///////////////////////////////////////////////////

    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $callback_url);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
    $res = curl_exec($ch);
    curl_close($ch);

    if($cid == 1){//口袋ATM
        $rs = json_decode($res,true);
        if($rs['succeed'] == 1){
            exec_db("INSERT INTO tttj.jfq_active (cid,appid,idfa,ctime) VALUES ('{$cid}','{$appid}','{$idfa}','{$now}')");
            exec_db("DELETE FROM tttj.jfq_callback WHERE id = {$id} LIMIT 1");
        }elseif(isset($rs['succeed']) && $rs['succeed'] == '0'){
            exec_db("DELETE FROM tttj.jfq_callback WHERE id = {$id} LIMIT 1");
        }else{
            dolog('jfq callback failed for id = ' . $id, $rs);
        }

    }else{

    }

}
